package com.mes.device.server.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.mes.common.pojo.MESResult;
import com.mes.common.util.Constant;
import com.mes.device.mapper.DeviceMapper;
import com.mes.device.mapper.UpkeepMapper;
import com.mes.device.mapper.UpkeepTypeMapper;
import com.mes.device.server.UpkeepServer;
import com.mes.pojo.device.Device;
import com.mes.pojo.device.Upkeep;
import com.mes.pojo.device.UpkeepType;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * @Author 张喆
 * @Company
 * @Description
 * @Time 2024/10/19 10:11
 */
@Service
public class UpkeepServerImpl implements UpkeepServer {
    @Autowired
    private UpkeepMapper upkeepMapper;
    @Autowired
    private DeviceMapper deviceMapper;
    @Autowired
    private UpkeepTypeMapper upkeepTypeMapper;

    @Override
    public MESResult queryAll(Upkeep upkeep, Integer page, Integer limit) {
        PageHelper.startPage(page,limit);
        List<Upkeep> upkeepList = upkeepMapper.selectAll(upkeep);
        PageInfo<Upkeep> upkeepPageInfo = new PageInfo<>(upkeepList);
        return MESResult.success(upkeepPageInfo);
    }

    @Override
    public MESResult queryById(String upkeepId) {
        List<String> connectDevice = upkeepMapper.selectConnectDevice(upkeepId);
        List<String> connectUpkeepType = upkeepMapper.selectConnectUpkeepType(upkeepId);
        List<Device> devices = new ArrayList<>();
        List<UpkeepType> upkeepTypes = new ArrayList<>();
        for (String connect : connectDevice) {
            devices.add(deviceMapper.selectByDeviceId(connect));
        }
        for (String upkeepType : connectUpkeepType) {
            upkeepTypes.add(upkeepTypeMapper.selectById(upkeepType));
        }
        Upkeep upkeep = upkeepMapper.selectByUpkeepId(upkeepId);
        upkeep.setDeviceList(devices);
        upkeep.setUpkeepTypeList(upkeepTypes);
        return MESResult.success(upkeep);
    }

    @Override
    public MESResult queryDesc(String upkeepId) {
        List<String> connectDevice = upkeepMapper.selectConnectDevice(upkeepId);
        List<String> connectUpkeepType = upkeepMapper.selectConnectUpkeepType(upkeepId);
        List<Device> devices = new ArrayList<>();
        List<UpkeepType> upkeepTypes = new ArrayList<>();
        for (String connect : connectDevice) {
            devices.add(deviceMapper.selectByDeviceId(connect));
        }
        for (String upkeepType : connectUpkeepType) {
            upkeepTypes.add(upkeepTypeMapper.selectById(upkeepType));
        }
        Upkeep upkeep = upkeepMapper.selectByUpkeepId(upkeepId);
        upkeep.setDeviceList(devices);
        upkeep.setUpkeepTypeList(upkeepTypes);
        return MESResult.success(upkeep);
    }

    @Override
    public MESResult deleteUpkeep(String upkeepId) {
        Upkeep upkeep = upkeepMapper.selectByUpkeepId(upkeepId);
        upkeep.setUpkeepStatus(99);
        return MESResult.success(upkeepMapper.updateUpkeep(upkeep));
    }

    @Override
    public MESResult updateUpkeep(Upkeep upkeep) {
        Upkeep same = upkeepMapper.selectByUpkeepName(upkeep.getUpkeepName());
        if (same != null && !same.getUpkeepId().equals(upkeep.getUpkeepId())){
            return MESResult.error(Constant.RESPONSE_STATUS_REPEAT);
        }
        return MESResult.success(upkeepMapper.updateUpkeep(upkeep));
    }

    @Override
    public MESResult insertUpkeep(Upkeep upkeep) {
        if (upkeep.equals(null) || upkeep.equals(" ")){
            return MESResult.error("缺少元素");
        }
        Upkeep same = upkeepMapper.selectByUpkeepName(upkeep.getUpkeepName());
        if (same != null){
            return MESResult.error(Constant.RESPONSE_STATUS_REPEAT);
        }
        String maxTypeId = upkeepMapper.selectMaxId();
        String[] split = maxTypeId.split("_");
        int num = Integer.parseInt(split[1]);
        num = num + 1;
        String strNum = null;
        if (num > 0 && num < 10){
            strNum = "00000" + num;
        }else if (num < 100){
            strNum = "0000" + num;
        }else if (num < 1000){
            strNum = "000" + num;
        }else if (num < 10000){
            strNum = "00" + num;
        }else if (num < 100000){
            strNum = "0" + num;
        }else {
            strNum = num + "";
        }
        upkeep.setUpkeepId(split[0] + "_" + strNum);
        return MESResult.success(upkeepMapper.insertUpkeep(upkeep));
    }

    @Override
    public MESResult insertConnectDevice(String upkeepId, String[] deviceIds) {
        List<String> deviceIdList = upkeepMapper.selectConnectDevice(upkeepId);
        for (String insertId : deviceIds) {
            Boolean exist = false;
            //避免重复输入
            for (String existId : deviceIdList) {
                if (existId.equals(insertId)){
                    exist = true;
                    break;
                }
            }
            if (!exist){
                upkeepMapper.insertUpkeepConnectDevice(upkeepId,insertId);
            }
        }
        return MESResult.success();
    }

    @Override
    public MESResult insertConnectUpkeepType(String upkeepId, String[] upkeepTypeIds) {
        List<String> upkeepTypeIdList = upkeepMapper.selectConnectUpkeepType(upkeepId);
        for (String upkeepTypeId : upkeepTypeIds) {
            System.out.println(upkeepTypeId);
        }
        for (String upkeepTypeId : upkeepTypeIds) {
            Boolean exist = false;
            //避免重复输入
            for (String existId : upkeepTypeIdList) {
                if (existId.equals(upkeepTypeId)){
                    exist = true;
                    break;
                }
            }
            if (!exist){
                upkeepMapper.insertUpkeepConnectUpkeepType(upkeepId,upkeepTypeId);
            }
        }
        return MESResult.success();
    }

    @Override
    public MESResult deleteConnectDevice(String upkeepId, String deviceId) {
        return MESResult.success(upkeepMapper.deleteUpkeepConnectDevice(upkeepId,deviceId));
    }

    @Override
    public MESResult deleteConnectUpkeepType(String upkeepId, String upkeepTypeId) {
        return MESResult.success(upkeepMapper.deleteUpkeepConnectUpkeepType(upkeepId,upkeepTypeId));
    }
}
